// 中间所有出的牌汇总 + 计时器
class centerOutPoker extends egret.Sprite{
    private previOutCardId:number = -1;//保存上一家出牌的座位ID
    public constructor(){
        super();
           this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAddEd,this);
    }
    private onAddEd(){
        this.init();
        this.once(egret.Event.REMOVED_FROM_STAGE,this.onRemove,this);
    }
    private onRemove(){
        egret.Tween.removeTweens(this.outPokerPoint);
    }

    private pointAndTimer:point;//计时器指向
    private outPokerPoint:movePoint = new movePoint();//出牌指向
    private outPokerList:Array<outPokerPace> = new Array();//出牌对象列表

    private NewestOutPokerXY:any;//保存最新出牌坐标及玩家ID

    private init(){
        this.pointAndTimer = new point();
        this.pointAndTimer.anchorOffsetX = this.pointAndTimer.width/2;
        this.pointAndTimer.anchorOffsetY= this.pointAndTimer.height/2;
        this.pointAndTimer.x = 0;
        this.pointAndTimer.y = 0;
        this.pointAndTimer.updateData(0,0);
        this.addChild(this.pointAndTimer);

        let obj_xy:Array<any>= [{x:-80,y:85},{x:85,y:-150},{x:-350,y:-215},{x:-258,y:-115}];
        for(var i=0;i<4;i++){
            let pokerObj:outPokerPace = new outPokerPace(i);
            if(i == 2){
                pokerObj.scaleX = 0.9;
                pokerObj.scaleY = 0.9;
                pokerObj.x = obj_xy[i].x+40;
                pokerObj.y = obj_xy[i].y+15;
            }else{
                pokerObj.x = obj_xy[i].x;
                pokerObj.y = obj_xy[i].y;
            }            
            this.addChild(pokerObj);
            this.outPokerList.push(pokerObj);
        }

        this.outPokerPoint.visible = false;
        this.addChild(this.outPokerPoint);
    }

    //刷新计时指向 (玩家座位ID)
    private lastseatedindex: any
    private lastOriginalPoint: any
    public updateTimerPoint(seatedindex:number,OriginalPoint:number){
        if (_.isEqual(seatedindex, this.lastseatedindex) && _.isEqual(OriginalPoint, this.lastOriginalPoint)) {
            console.log('updateTimerPoint isEqual')
            return 
        } else {
            this.lastseatedindex = seatedindex
            this.lastOriginalPoint = OriginalPoint
            this.pointAndTimer.updateData(seatedindex,OriginalPoint);
        }
    }
    //删除最新出的牌
    public deleteNewOutCard(){
       // StageData.GameRoot.clew("删除桌上被赢的牌："+this.previOutCardId);
        if(this.previOutCardId >-1){   
           // StageData.GameRoot.clew("执行桌上被赢的牌："+GameData.transformSeatIndex[this.previOutCardId]);           
            this.outPokerList[this.previOutCardId].deletSinglePoker();
            if(this.outPokerPoint){
                this.outPokerPoint.visible = false;
            }            
        }        
    }
    //获取要出牌坐标 (玩家座位ID)
    // public getOutPoker_xy(seatedindex:number):any{
    //     var lsobj:any = this.outPokerList[seatedindex].getOutPoketXY();      
    //     console.log('getOutPoker_xy', lsobj);
    //     var addx:number=0;
    //     var addy:number=0;
    //     var lsx:number;
    //     var lsy:number;
    //     if(seatedindex == 2){
    //         addx = Math.floor(431*this.outPokerList[seatedindex].scaleX);
    //         if(lsobj.conut == 0){
    //             lsx = lsobj.x + this.outPokerList[seatedindex].x-(438-addx);
    //         }else{
    //             let lsconut:number = (lsobj.conut-1)%(9);
    //             lsx = lsobj.x + this.outPokerList[seatedindex].x-(438-addx)+((lsconut)*6)-lsconut+10;
    //         }            
    //     }else{
    //         addx = 0;
    //         lsx = lsobj.x+this.outPokerList[seatedindex].x;
    //     }
    //     lsy = lsobj.y+this.outPokerList[seatedindex].y;
    //     console.log('getOutPoker_xy', seatedindex);
    //     // if (lsobj.conut > 18) {
    //     //     lsx = 0
    //     //     lsy = 0
    //     // }
    //     this.NewestOutPokerXY = {x:lsx,y:lsy,seatedindex:seatedindex};
    //     return {x:lsx,y:lsy};//返回牌的坐标;
    // }

    public getOutPoker_xy(seatedindex:number):any{
        var lsobj:any = this.outPokerList[seatedindex].getOutCard();      
        var outNumber = this.outPokerList[seatedindex].getOutNumber()
        console.log('getOutPoker', lsobj);
        if (lsobj) {
            var position = lsobj.localToGlobal()
            if (outNumber > 18) {
                return {
                    // x: position.x - 583,
                    // y: position.y - 360,
                    x: position.x - 565,
                    y: position.y - 310,
                    seatedindex:seatedindex
                }
            } 
            return {
                // x: position.x - 583,
                // y: position.y - 360,
                x: position.x - 560,
                y: position.y - 310,
                seatedindex:seatedindex
            }
        }
        return {x:0,y:0, seatedindex:seatedindex};//返回牌的坐标;
    }

    private lastuserList: any = []
    private lastpointList: any
    private lastlastCard: any
    //添加牌数字（房间信息数据，转换过的方向数组）
    public addPokerList(userList:any,pointList:Array<number>,lastCard:any){
        console.log('addPokerList')
        if (_.isEqual(userList.userList, this.lastuserList) && _.isEqual(pointList, this.lastpointList)) {
            console.log('centeroutpoker addPokerList isEqual')
        } else {
            // this.clearAllPoker();
            // for(var i=0;i<userList.userList.length;i++){                   
            //         if(userList.userList[i].showList&&userList.userList[i].showList.length>0){
            //             let lslength:number = userList.userList[i].showList.length;
            //             for(var n=0;n<userList.userList[i].showList.length;n++){//"cardIndex":13,"count":3,"suitType":"S"
            //                 let lspoker = userList.userList[i].showList[n];
            //                 this.outPokerList[pointList[userList.userList[i].seatIndex]].addSinglePoker(lspoker.suitType,lspoker.count,lspoker.cardIndex);
            //             }
            //         }
            // }
            // this.lastuserList = userList
            // this.lastpointList = pointList

            for(var i=0;i<userList.userList.length;i++){      
                if(userList.userList[i].showList){
                    console.log('userList.userList[i].showList', userList.userList[i].showList);
                    let showListNotChanged = _.isEqual(userList.userList[i].showList, this.lastuserList[i] ? this.lastuserList[i].showList : [])
                    if (showListNotChanged) {
                        console.log('player' + i + ' showListNotChanged')
                        continue 
                    } else {
                        console.log('player' + i + ' showListChanged')
                        console.log(userList.userList[i].showList, this.lastuserList[i] ? this.lastuserList[i].showList : [])
                    }
                    this.outPokerList[pointList[userList.userList[i].seatIndex]].clearAllPoker();
                    // let lslength:number = userList.userList[i].showList.length;
                    for(var n=0;n<userList.userList[i].showList.length;n++){//"cardIndex":13,"count":3,"suitType":"S"
                        let lspoker = userList.userList[i].showList[n];
                        this.outPokerList[pointList[userList.userList[i].seatIndex]].addSinglePoker(lspoker.suitType,lspoker.count,lspoker.cardIndex);
                    }
                }
            }
        }
        
        console.log('lastCard', lastCard);
        if (_.isEqual(lastCard, this.lastlastCard)) {
            console.log('lastCard isEqual', lastCard);     
        } else {
            if(lastCard){
                this.NewestOutPokerXY = this.getOutPoker_xy(pointList[lastCard.seatIndex]); 
            }else{
                this.outPokerPoint.visible = false;
            }  
            this.lastlastCard = lastCard
        }

        // if (_.isEqual(userList, this.lastuserList) && _.isEqual(pointList, this.lastpointList) && _.isEqual(lastCard, this.lastlastCard)) {
        //     console.log('allEqual')
        // } 
        this.updateOutPokert_xy()

        this.lastuserList = userList.userList
        this.lastpointList = pointList
    }

    //添加一张打出的牌 (玩家座位号，花色，面值，牌ID号)
    public addSinglePoker(seatedindex:number,flower:string,value:string,id:number){
        if(seatedindex >= -1){
           this.previOutCardId = seatedindex;
        }
        this.outPokerList[seatedindex].addSinglePoker(flower,value,id);
        this.getOutPoker_xy(seatedindex)
        var userInfo 
        switch (seatedindex) {
            case 0:
                userInfo = this.lastuserList.find(user => user.seatIndex === GameData.mySeatIndex)
            break
            case 1:
                userInfo = this.lastuserList.find(user => user.seatIndex === GameData.mySeatIndex + 1)
            break
            case 3:
                userInfo = this.lastuserList.find(user => user.seatIndex === GameData.mySeatIndex - 1)
            break
            case 2:
                userInfo = this.lastuserList.find(user => user.seatIndex !== GameData.mySeatIndex &&
                  user.seatIndex !== GameData.mySeatIndex + 1 &&
                user.seatIndex !== GameData.mySeatIndex - 1)
            break
        }
        console.log('userInfo', userInfo);
        if (userInfo) {
            userInfo.showList.push({flower, value, id})
        }
        this.updateOutPokert_xy();
        StageData.soundObj.soundSinglePlay("outCard");
        StageData.soundObj.soundSinglePlay(flower+"_"+value);
        GameData.lastOutPoker = {
            flower,
            value
        }
    }

    //显示查找的牌
    public showlookupPoker(pokerflower:number,pokerValue:number){
        for(var i=0;i<4;i++){
            this.outPokerList[i].showlookupPoker(pokerflower,pokerValue);
        }
    }
    //隐藏查找的牌
    public hidelookupPoker(){
        for(var i=0;i<4;i++){
            this.outPokerList[i].hidelookupPoker();
        }
    }
    //清除所有出的牌;
    public clearAllPoker(){
        for(var i=0;i<4;i++){
            this.outPokerList[i].clearAllPoker();
        }
    }

    public clearPointer() {
        this.outPokerPoint.visible = false;
        this.outPokerPoint.x = 0;
        this.outPokerPoint.y = 0;        
        this.NewestOutPokerXY = null;
    }

    //刷新出牌指向坐标
    private SignPoker_xy:Array<any> = [{x:23.5,y:34.5},{x:34.5,y:23.5},{x:23.5,y:34.5},{x:34.5,y:23.5}];
    private updateOutPokert_xy(){
        console.log('NewestOutPokerXY', this.NewestOutPokerXY);
        if (!this.NewestOutPokerXY) {
            return
        }
        const {x, y} = this.NewestOutPokerXY
        // console.log(this.outPokerPoint.x, this.outPokerPoint.y);
        // if (this.outPokerPoint.x === 0 && this.outPokerPoint.y === 0) {
        //     this.outPokerPoint.visible = false;
        // }
        if (this.outPokerPoint && this.NewestOutPokerXY) {
            // egret.Tween.get(this.outPokerPoint).wait(100).to({x,y},300,egret.Ease.quadOut);
            console.log('outPokerPoint...', x, y);
            this.outPokerPoint.x = x
            this.outPokerPoint.y = y
            this.outPokerPoint.visible = true;
        }
        
        // if(this.NewestOutPokerXY){
        //     var lsx:number = this.NewestOutPokerXY.x+this.SignPoker_xy[this.NewestOutPokerXY.seatedindex].x;
        //     var lsy:number = this.NewestOutPokerXY.y+this.SignPoker_xy[this.NewestOutPokerXY.seatedindex].y;
        //     egret.Tween.get(this.outPokerPoint).to({x:lsx,y:lsy},300,egret.Ease.quadOut);
        // } else if (this.outPokerPoint.x === 0 && this.outPokerPoint.y === 0) {
        //     this.outPokerPoint.visible = false;
        // }
    }
}